
#include "common.hpp"
#include <memory>
#include <iostream>
#include <string>
#include "socket.hpp"
#include "Protocol.hpp"
using namespace socketmodule;
void Getdata(int& x,int& y,char& opera)
{
         std::cout<<"please enter x:";
        std::cin>>x;
         std::cout<<"please enter y:";
        std::cin>>y;
         std::cout<<"please enter operator:";
        std::cin>>opera;
}
void usage(char* proc)
{
    std::cout<<"you should use like this:"<<proc<<"server_ip,server_port"<<std::endl;
}
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        usage(argv[0]);
        exit(1);
    }
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    std::shared_ptr<Socket> client = std::make_shared<Tcpsocket>();
    std::string resp_str ;
    client->buildTcpclientsocket();
    if(client->Connecting(port,ip)!=0)
    {
        //到这个地方成功了
        std::cerr<<"connect error"<<std::endl;
        exit(4);
    }
    std::unique_ptr<Protocol> protocol = std::make_unique<Protocol>();
    //连接成功啦
    while(true)
    {
        int x,y;
        char opera;
        Getdata(x,y,opera);
        //获取数据之后，构建请求；
        std::string reqs = protocol->BuildQequeststring(x,y,opera);
        client->Send(reqs);//发送请求
        //获取应答与处理应答
        Response resp;
        protocol->Getresponse(client,resp_str,&resp);
    }
    client->close();
    return 0;
}